package consultan.vanke.com.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import consultan.vanke.com.R;
import consultan.vanke.com.activity.MainActivity;
import consultan.vanke.com.adapter.CommonAdapter;
import consultan.vanke.com.adapter.ViewHolder;
import consultan.vanke.com.bean.HomeJumpBean;
import consultan.vanke.com.constant.ConstantLibrary;
import consultan.vanke.com.event.MessageEvent;
import consultan.vanke.com.utils.SimpleToolbar;
import qiu.niorgai.StatusBarCompat;

import static com.bytedance.sdk.account.open.aweme.utils.ViewUtils.getStatusBarHeight;
import static consultan.vanke.com.utils.StatusBarUtil.setPaddingTop;
import static consultan.vanke.com.utils.StatusBarUtil.setRootView;


public class MyMoneyFragment extends BaseFragment {
    @BindView(R.id.simple_toolbar)
    SimpleToolbar simpleToolbar;
    @BindView(R.id.simple_toolbar_main)
    ConstraintLayout simpleToolbarMain;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.lienar_top)
    LinearLayout lienarTop;
    @BindView(R.id.rel_content)
    RelativeLayout relContent;
    @BindView(R.id.recy_list)
    RecyclerView recyList;
    @BindView(R.id.linear_search)
    LinearLayout linearSearch;
    private View inflate;
    private boolean isfristshow = true;
    private CommonAdapter<String> adapterCenter;
    private CommonAdapter<String> adapterPop;
    private List<String> subjectsBeansCenter = new ArrayList<>();
    private List<String> subjectsBeansPop = new ArrayList<>();
    private PopupWindow popupWindow;
    private View def_sort;

    @Override
    protected void initViews() {
        simpleToolbar.setMainTitle("余额");
        def_sort = LayoutInflater.from(_mActivity).inflate(R.layout.money_sort_def, null);
        TextView tv_canle = def_sort.findViewById(R.id.tv_canle);
        tv_canle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        RecyclerView recy_list = def_sort.findViewById(R.id.recy_list);
        recy_list.setLayoutManager(new GridLayoutManager(_mActivity, 3));
        for (int i = 0; i < 9; i++) {
            subjectsBeansPop.add("11");
        }
        adapterPop = new CommonAdapter<String>(_mActivity, subjectsBeansPop, R.layout.pop_item) {
            @Override
            public void onBindViewHolder(ViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
            }

            @Override
            public void convert(ViewHolder holder, String assetBean) {
//                ImageView img_photo = holder.getView(R.id.img_photo);
            }

        };
        recy_list.setAdapter(adapterPop);
        popupWindow = new PopupWindow(def_sort, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
    }

    @Override
    protected void initDatas() {
        for (int i = 0; i < 10; i++) {
            subjectsBeansCenter.add("111");
        }
        recyList.setLayoutManager(new LinearLayoutManager(_mActivity));
        adapterCenter = new CommonAdapter<String>(_mActivity, subjectsBeansCenter, R.layout.money_item) {
            @Override
            public void onBindViewHolder(ViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
            }

            @Override
            public void convert(ViewHolder holder, String assetBean) {
//                ImageView img_photo = holder.getView(R.id.img_photo);
            }

        };
        recyList.setAdapter(adapterCenter);
    }

    @Override
    protected void initEvents() {
        simpleToolbar.setLeftTitleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });
        simpleToolbar.setRightTitleAginClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start(new MyMoneyFragment());
            }
        });
    }

    @Override
    protected View initContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        inflate = View.inflate(_mActivity, R.layout.my_money_fragment, null);
        return inflate;
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        ViewGroup decorView = (ViewGroup) _mActivity.getWindow().getDecorView();
        View fakeStatusBarView = new View(_mActivity);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(_mActivity) + 3);
        fakeStatusBarView.setLayoutParams(params);
        fakeStatusBarView.setBackground(getResources().getDrawable(R.drawable.fast_regist_title_bg));
        fakeStatusBarView.setId(android.R.id.custom);
        decorView.addView(fakeStatusBarView);
        setRootView(_mActivity, false);
        if (isfristshow) {
            setPaddingTop(_mActivity, simpleToolbar);
            isfristshow = false;
        }
        StatusBarCompat.translucentStatusBar(_mActivity, true);
    }

    @OnClick({R.id.btn_login, R.id.linear_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                MessageEvent<HomeJumpBean> messageEvent = new MessageEvent<>();
                messageEvent.setData(new HomeJumpBean(ConstantLibrary.chongzhiFragment, ConstantLibrary.TYPE_TIME_OUT));
                EventBus.getDefault().post(messageEvent);
                break;
            case R.id.linear_search:
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    return;
                }
                popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
//                        imgNorsort.setBackgroundDrawable(getResources().getDrawable(R.mipmap.asset_buttom));
                    }
                });
                showAsDropDowns(popupWindow);
                break;
        }
    }

    public void showAsDropDowns(PopupWindow popupWindow) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            relContent.getWindowVisibleDisplayFrame(rect);
            Rect outRect1 = new Rect();
            _mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);
            int h = outRect1.height() - rect.bottom;
            popupWindow.setHeight(h);
            popupWindow.showAsDropDown(relContent, 0, 0);
        } else {
            popupWindow.showAsDropDown(relContent, 0, 0);
        }
    }
}
